////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.menu {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	
	import kiwi.utils.Validation;
	
	import plantool.core.CoreModule;
	import plantool.modules.menu.display.MenuGroup;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class Menu extends CoreModule {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private static var _instance:Menu			= null;
		private var _container:MovieClip			= null;
		private var _containerMask:Shape			= null;
		private var _isInitialized:Boolean			= false;
		private var _groups:Vector.<MenuGroup>		= new Vector.<MenuGroup>();
		private var _onCompleteHandler:Function		= null;
		private var _width:Number					= 120;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public override function get width():Number {
			return _width;
		}
		public override function set width(value:Number):void {
			//set width
			_width = value;
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function Menu(pvt:PrivateConstructorClass) {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		public static function getInstance():Menu {
			//check if instance exists, if not create one
			if (!Validation.isSet(_instance)) {
				_instance = new Menu(new PrivateConstructorClass());
			}
			
			//return instance
			return _instance;
		}
		
		private function drawGroups():void {
			//create existing groups
			for (var i:uint = 0; i < menuApi.groups.length; i++) {
				//create group
				var group:MenuGroup = new MenuGroup(menuApi.groups[i]);
				group.y				= 10 + (_groups.length * group.height);
				group.width			= _width;
				_container.addChild(group);
				
				//store menugroup
				_groups.push(group);
			}
		}
		
		private function initialized():void {
			//set initialized flag
			_isInitialized = true;
			
			//listen to page invalidate to position this module
			base.stage.addEventListener(Event.RESIZE, onResize_stage);
			onResize_stage(null);
			
			//make this module visible
			Tweener.addTween(this, { _autoAlpha:1 });
			
			//database intact, call onCompleteHandler
			if (Validation.isSet(_onCompleteHandler)) {
				_onCompleteHandler();
			}
		}
		
		//TODO: Fix Autosizing Menu
		private function onResize_stage(e:Event):void {
			//draw background
			graphics.clear();
			graphics.beginFill(0xFFFFFF);
			graphics.drawRoundRect(0, 0, _width + 10, base.stage.stageHeight - 150, 10, 10);
			graphics.endFill();
			
			//draw container mask
			_containerMask.graphics.clear();
			_containerMask.graphics.beginFill(0xFFFFFF);
			_containerMask.graphics.drawRoundRect(0, 0, _width + 10, base.stage.stageHeight - 150, 10, 10);
			_containerMask.graphics.endFill();
			
			this.x	= base.stage.stageWidth - this.width;
			this.y	= 25;
		}
		
		protected override function init():void {
			//create container
			_container = new MovieClip();
			addChild(_container);
			_containerMask = new Shape();
			addChild(_containerMask);
			_container.mask = _containerMask;
			
			//create bevel filter
			this.filters = [new DropShadowFilter(0, 45, 0x666666, 1, 8, 8, 0.5, 3, true)];
			
			//draw groups
			drawGroups();
			
			//initialized
			initialized();
		}
		
		public override function show(onCompleteHandler:Function=null):void {
			//check if we're initialized
			if (!_isInitialized) {
				//store oncompletehandler
				_onCompleteHandler	= onCompleteHandler;
			} else {
				//call oncompletehandler
				if (Validation.isSet(onCompleteHandler)) {
					onCompleteHandler();
				}
			}
		}
	}
}

//--------------------------------------------------------------------------
//END OF PACKAGE
//Define the PrivateConstructorClass here so an instance cannot be by anyone
//except for the class defined in the above package
//--------------------------------------------------------------------------
class PrivateConstructorClass {
	//--------------------------------------------------------------------------
	//Constructor and Destructor
	//--------------------------------------------------------------------------
	public function PrivateConstructorClass() {
	}
}